<?php

namespace App\Logic\finger;

use App\Logic\Code;
use App\Logic\point\Point;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;


/**
 * @author AdamTyn
 * @description <用户>数据模型
 */
class FilLogic
{


    //添加
    public static function transfer($data,$user,$scene){

        $validateRule = [
            'user_name' => 'required',
            'amount' => 'required|gt:0',
            'code' => 'required',
        ];
        $errorMsg = [
            'user_name.required' => '请传入用户名 user_name',
            'amount.required' => '请传入积分数量 amount',
            'amount.gt' => '请传入有效积分 amount',
            'code.required' => '请传入验证码 code',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }


        $key = $user['phone'].'_code_'.$scene;

        $code_info = Code::code($key,$data['code']);
        if($code_info['status'] === 'ERROR'){
            return $code_info;
        }

        $shift = Db::table('user')
            ->where('phone', $data['user_name'])
            ->select('username','phone','id')
            ->first();

        if(!$shift){
            $shift = Db::table('user')
                ->where('username', $data['user_name'])
                ->select('username','phone','id')
                ->first();
        }
        if(!$shift){
            return e_msg(123456,'用户信息不存在');
        }elseif($shift->id == $user['id']){
            return e_msg(300029,'你不能转给自己');
        }

        if ($user['fil'] < $data['amount']) {
            return e_msg(300026, '转账余额不足'.$user['fil']);
        }

        $transfer_min = $data['amount'] * Cache::get('configs')['min']['transfer']->val;
        if($data['amount'] < $transfer_min){
            return e_msg(300026, '转账数目不足最小金额');
        }

        $fil_insert_arr[] = [
            'out_account' => $user['username'],
            'out_phone' => $user['phone'],
            'shift_account' => $shift->username,
            'shift_phone' =>  $shift->phone,
            'amount' => -$data['amount'],
            'type_note' => '转出',
            'type' => 'out',
        ];
        $fil_insert_arr[] = [
            'out_account' => $shift->username,
            'out_phone' =>  $shift->phone,
            'shift_account' => $user['username'],
            'shift_phone' => $user['phone'],
            'amount' => $data['amount'],
            'type_note' => '转入',
            'type' => 'shift',
        ];


        //用户转出用户，转入用户修改  转出，转入记录表  srt手续费
        DB::beginTransaction();
        $rel1 = DB::table('user')->where('id', $shift->id)->increment('fil', $data['amount']);
        $rel2 = DB::table('user')->where('id', $user['id'])->decrement('fil', $data['amount']);
        $rel3 = DB::table('record_eth')->insert($fil_insert_arr);
        $rel4 = DB::table('record_srt')->insert($fil_insert_arr);
        if($rel1&&$rel2&&$rel3&&$rel4){
            DB::commit();
            return s_msg('');
        }else{
            DB::rollBack();
            return e_msg(123456,'有问题');
        }


    }


    //系统修改
    public static function transfer_sys($data){

        $validateRule = [
            'id' => 'required',
            'amount' => 'required',
            'token' => 'required',
        ];
        $errorMsg = [
            'id.required' => '请传入id id',
            'amount.required' => '请传入划转数量 amount',
            'token.required' => '请传入币种信息 token:FIL or RED or POWER',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }
        $coin_type = strtolower($data['token']);

        if($data['amount'] > 0){
            $type = 'sys_shift';
        }else{
            $type = 'sys_out';
        }

        if($coin_type == 'power'){


            $mh = DB::table('miner_host')->where('mid',$data['id'])->first();
            if(!$mh){
                return e_msg(123456,'矿机不存在');
            }
            $mi = DB::table('miner_info')->where("mid",$data['id'])->first();
            if(!$mi){
                return e_msg(123456,'矿机不存在');
            }
            $new_host = $mh->hspace + $data['amount'];
            if($new_host < 10){
                return e_msg(123456,'该矿机容量不能小于10');
            }

            $mu = DB::table('miner_user')->where('mid',$data['id'])->first();
            if(!$mu){
                return e_msg(123456,'该矿机没有绑定用户');
            }

            $time = time();
            $host = DB::table('miner_host')
                ->where('token','FIL')
                ->where('end_time','>',$time)
                ->sum('hspace');
            $all_host = $host + $data['amount'];
            $origin = Point::curl();
            if($origin['status'] == 'ERROR'){
                return $origin;
            }else{
                if($origin['data']['code'] == 0 ){
                    return e_msg(3202300,'请求您的总容量失败');
                }else{
                    if($all_host>$origin['data']['data']){
                        return e_msg(3202300,'可用总容量为'.$origin['data']['data'].'已超出限额，请联系官方客服扩容');
                    }
                }
            }
            $fil_insert_arr = [
                'out_account' => $mu->user_name,
                'out_phone' => $mi->m_number,
                'shift_account' => '系统',
                'shift_phone' => '系统',
                'amount' => $data['amount'],
                'type_note' => '系统修改',
                'type' => $type,
                'coin_type' => $coin_type,
            ];


        }else{

            $user = DB::table('user')->where('id',$data['id'])->select('username','phone')->first();
            if(!$user){
                return e_msg(123456,'该用户不存在');
            }
            $fil_insert_arr = [
                'out_account' => $user->username,
                'out_phone' => $user->phone,
                'shift_account' => '系统',
                'shift_phone' => '系统',
                'amount' => $data['amount'],
                'type_note' => '系统修改',
                'type' => $type,
                'coin_type' => $coin_type,
            ];

        }

        //用户表添加fil，用户钱包表记录，fil表记录   或  矿机修改hspace   fil表记录
        DB::beginTransaction();

        if($coin_type == 'power'){
            $rel1 = DB::table('miner_host')->where('mid',$data['id'])->increment('hspace',$data['amount']);
            $rel2 = DB::table('record_fil')->insert($fil_insert_arr);
        }else{
            $rel1 = DB::table('user')->where('id',$data['id'])->increment($coin_type,$data['amount']);
            $rel2 = DB::table('record_fil')->insert($fil_insert_arr);
        }
        if($rel1&&$rel2){
            DB::commit();
            return s_msg('');
        }else{
            DB::rollBack();
            return e_msg(123456,'有问题');
        }


    }



}
